package com.mdms.init;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Hibernate;
import org.hibernate.Transaction;

import com.mdms.commands.BaseCommand;
import com.mdms.dto.MdmMenuConf;
import com.mdms.dto.MdmResources;
import com.mdms.interfaces.orm.CrudDAO;
import com.mdms.logger.SimplyMDLogger;

public class GetMenuCommand extends BaseCommand {

	private static final long serialVersionUID = 1L;


	private  List<MdmMenuConf> menus;

	private final static SimplyMDLogger logger = new SimplyMDLogger(GetMenuCommand.class);

	@Override
	public void execute() throws Exception {
		logger.debug("execute", "START");
		try{
			CrudDAO crudDAO = getDaoFactory().getCrudDAO();
			crudDAO.setPersistentClass(MdmMenuConf.class);
			Transaction tx= crudDAO.beginTransaction();
			List <MdmMenuConf>result = crudDAO.findAll();
			if(result!=null && result.size()>0){
				for (MdmMenuConf mdmMenuConf : result) {
					Hibernate.initialize(mdmMenuConf.getMdmMenuConf());
				}
				menus=result;
			tx.commit();
			}else{
				setErrorCode(-1);
				setErrorDesc("No database resources");
			}
		}catch(Exception e){
			setErrorCode(-1);
			setErrorDesc(e.getMessage());
			logger.error("execute", e);
		}
		logger.debug("execute", "START");
	}

	public List<MdmMenuConf> getMenus() {
		return menus;
	}



}
